package cn.wikiflyer.beikang.act.user.setting;

import java.lang.reflect.Type;

import net.tsz.afinal.annotation.view.ViewInject;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import cn.wikiflyer.beikang.BaseActivity;
import cn.wikiflyer.beikang.IApplication;
import cn.wikiflyer.beikang.R;
import cn.wikiflyer.beikang.bean.base.BKBaseBean;
import cn.wikiflyer.beikang.utils.task.GetVerifyCodeTask;
import cn.wk.libs4a.WKApplication;
import cn.wk.libs4a.net.WKAsyncTaskPro;
import cn.wk.libs4a.utils.WKCountDownUtils;
import cn.wk.libs4a.utils.WKUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

/**
 * 注册
 * 
 * @author qunlee_mr
 * 
 */
public class UserSettingRegisterAct extends BaseActivity {

	@ViewInject(id = R.id.register_tel)
	private EditText telEdit;
	@ViewInject(id = R.id.register_pwd)
	private EditText pwdEdit;
	@ViewInject(id = R.id.register_code)
	private EditText codeEdit;
	@ViewInject(id = R.id.register_invitecode)
	private EditText inviteCodeEdit;

	@ViewInject(id = R.id.register_get_codebtn, click = "onClick")
	private Button getCodeBtn;
	@ViewInject(id = R.id.register_commit, click = "onClick")
	private Button registerBtn;
	// @ViewInject(id = R.id.register_find, click = "onClick")
	// private Button findBtn;
	// @ViewInject(id = R.id.register_login, click = "onClick")
	// private Button loginBtn;

	private Intent intent;

	@Override
	public void loadXml() {
		setContentView(R.layout.bk_act_user_setting_register);
	}

	@Override
	public void init() {
		app().cdUtils.setBtn(getCodeBtn);
	}

	@Override
	public void setData() {

	}

	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.register_find: // 找回密码
			intent = new Intent(ctx, UserSettingFindPwdAct.class);
			startActivity(intent);
			break;
		case R.id.register_login: // 去登录
			this.finish();
			// intent = new Intent(ctx, UserSettingLoginAct.class);
			// startActivity(intent);
			break;
		case R.id.register_get_codebtn: // 获取验证码
			registerTestAction(false);
			break;
		case R.id.register_commit: // 注册提交
			registerTestAction(true);
			break;

		default:
			break;
		}
	}

	// 注册验证
	private void registerTestAction(boolean is_register) { // is_register：TRUE
															// 注册提交验证，FALSE
															// 获取验证码验证手机号
		if (TextUtils.isEmpty(telEdit.getText())) {
			WKApplication.showToast("请输入手机号");
			return;
		}
		if (!WKUtils.isTel(telEdit.getText().toString())) {
			WKApplication.showToast("手机号码格式不正确");
			return;
		}
		if (!is_register) {
			new GetVerifyCodeTask(this, telEdit.getText().toString());
			// 获取验证码
			app().cdUtils.start();
			return;
		}
		if (TextUtils.isEmpty(pwdEdit.getText())) {
			WKApplication.showToast("请输入密码");
			return;
		}
		if (TextUtils.isEmpty(codeEdit.getText())) {
			WKApplication.showToast("请输入手机验证码");
			return;
		}
		if (TextUtils.isEmpty(inviteCodeEdit.getText())) {
			WKApplication.showToast("请输入注册邀请码");
			return;
		}

		// 访问网络提交注册信息

		new GetRegisterTask(this);
	}

	/**********************************************************************
	 * 数据接口
	 ***********************************************************************/
	/**
	 * 登录
	 */
	class GetRegisterTask extends WKAsyncTaskPro {
		public GetRegisterTask(Context context) {
			super(context);
		}

		@Override
		protected String doInBackground(boolean... params) {
			String mobile = telEdit.getText().toString();
			String password = pwdEdit.getText().toString();
			String mobile_verify = codeEdit.getText().toString();
			String parent_code = inviteCodeEdit.getText().toString();
			return app().net.register(mobile, password, mobile_verify,
					parent_code);
		}

		@Override
		protected void doOnPostExecute(String result, boolean isLocal) {
			Type cvbType = new TypeToken<BKBaseBean>() {
			}.getType();
			Gson gson = new Gson();
			BKBaseBean base = gson.fromJson(result, cvbType);
			if (base.status == 1) {
				Toast.makeText(ctx, "注册成功", Toast.LENGTH_LONG).show();
				activity.finish();
			} else {
				IApplication.showToast(base.info + "");
			}
		}
	}

}
